package com.cenwangkeji.resident.ui.activity;

import android.webkit.WebView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.CommunityDetailBean;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

import static com.cenwangkeji.resident.utils.HtmlUtils.getHtmlData;

//社区公告详情
public class CommunityDetailActivity extends BaseActivity {

    @BindView(R.id.wv)
    WebView mWv;

    @Override
    public int setLayoutId() {
        return R.layout.activity_community_detail;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        getData();

    }

    private void getData(){
        showLoading();
        final int id = getIntent().getIntExtra("id", 0);
        Observable.create(new ObservableOnSubscribe<CommunityDetailBean>() {
            @Override
            public void subscribe(ObservableEmitter<CommunityDetailBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( "id",id);
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.community_notice_Detail);
                MyLogger.showLargeLog("社区公告详情  "+json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json,CommunityDetailBean.class));
            }
        }).compose(RxUtils.<CommunityDetailBean>io_main()).subscribe(new ObserverOnNext<CommunityDetailBean>() {
            @Override
            public void onNext(CommunityDetailBean communityDetailBean) {
                if (communityDetailBean.getData()!= null){
                    mWv.loadDataWithBaseURL(null,
                            getHtmlData(communityDetailBean.getData().getContent()), "text/html", "utf-8", null);
                }else {
                    MyToast.show(communityDetailBean.getMsg());
                }
                    dismissLoading();

            }
        });
    }



}
